﻿using System;
using System.Management;
using System.Text;

namespace HyperV
{
    partial class VMHelper
    {

        public  ManagementObject AttachVHDtoVirtualSystem(String vmName, String vhdPath)
        {
            return AttachVHDtoVirtualSystem(Scope, vmName, vhdPath);
        }

        private static void RemoveSyntheticDiskIfExist(ManagementScope scope, ManagementObject vm) {
            ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
            ManagementObject syntheticDisk = Utility.GetResourceAllocationSettingData(vm, ResourceType.Disk, ResourceSubType.DiskSynthetic, null);
            if (syntheticDisk != null)
            {
                ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("RemoveVirtualSystemResources");
                inParams["TargetSystem"] = vm.Path.Path;
                inParams["ResourceSettingData"] = new String[] {syntheticDisk.GetText(TextFormat.CimDtd20)};

                Utility.SyncJob(scope, virtualSystemService.InvokeMethod("RemoveVirtualSystemResources", inParams, null));
           }
        }

        public static ManagementObject AttachVHDtoVirtualSystem(ManagementScope scope, String vmName, String vhdPath)
        {
            ManagementObject virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
            ManagementObject syntheticNic = null;
            ManagementObject vm = Utility.GetTargetComputer(vmName, scope);
            

            ManagementObject ideController = Utility.GetResourceAllocationSettingData(vm, ResourceType.IDEController, ResourceSubType.IDEController, null);

            ManagementObject existSyntheticDisk = Utility.GetResourceAllocationSettingData(vm, ResourceType.Disk, ResourceSubType.DiskSynthetic, null);

            ManagementObject syntheticDisk = Utility.GetResourceAllocationSettingDataDefault(scope, ResourceType.Disk, ResourceSubType.DiskSynthetic, null);
            syntheticDisk["Parent"] = ideController.Path.Path;
            syntheticDisk["Address"] = 0;

            //ManagementObject SyntheticDisk = Utility.GetResourceAllocationsettingDataDefault(scope, ResourceType.Disk, ResourceSubType.DiskSynthetic, null);

            StringBuilder sb = new StringBuilder();

            //foreach (var x in hardDisk.Properties)
            //{
            //    sb.AppendLine(x.Name + "   " + x.Value);
            //}

            string s = sb.ToString();

            ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("AddVirtualSystemResources");
            inParams["ResourcesettingData"] = new String[] {syntheticDisk.GetText(TextFormat.CimDtd20)};
            inParams["TargetSystem"] = vm.Path.Path;

            ManagementBaseObject outParams = virtualSystemService.InvokeMethod("AddVirtualSystemResources", inParams, null);

            Utility.SyncJob(scope, outParams);

            ManagementObject addedSyntheticDisk = new ManagementObject(((string[])outParams["NewResources"])[0]);

            ManagementObject hardDisk = Utility.GetResourceAllocationSettingDataDefault(scope, ResourceType.StorageExtent, ResourceSubType.VHD, null);
            hardDisk["Parent"] = addedSyntheticDisk.Path.Path;
            hardDisk["Connection"] =  new string[] { vhdPath };

            
            ManagementBaseObject inParams2 = virtualSystemService.GetMethodParameters("AddVirtualSystemResources");
            inParams2["ResourcesettingData"] = new String[] {hardDisk.GetText(TextFormat.CimDtd20)};
            inParams2["TargetSystem"] = vm.Path.Path;

            Utility.SyncJob(scope, virtualSystemService.InvokeMethod("AddVirtualSystemResources", inParams2, null));

            //addedResources = (string[])outParams["NewResources"];
            //syntheticNic = new ManagementObject(addedResources[0]);


            inParams.Dispose();
            outParams.Dispose();
            vm.Dispose();
            syntheticDisk.Dispose();

            return syntheticNic;
        }
    }
}